<?php
 
namespace app\common\job;
 
use Yii;
use yii\base\BaseObject;
use app\helpers\DatetimeHelper;
use yii\helpers\ArrayHelper;
use app\modules\survey\service\SurveyInfoService;
use app\modules\survey\service\SurveyRangeService;
use app\modules\survey\service\SurveyQuestionService;
use app\modules\survey\service\SurveyQuestionOptionService;
use app\modules\survey\service\SurveyInfoLatestService;
use app\modules\survey\service\SurveyRangeLatestService;
use app\modules\notify\service\RemindInfoService;
use app\modules\inspect\service\InspectInfoService;
 
class SurveyAddJob extends BaseObject implements \yii\queue\Job
{
    public $surveyId;
    public $surveyRange;
    public $surveyInfo;
    public $publisher_user_id;
    public $createTimeStamp;
    public $surveyQuestion;

    public function execute($queue){
        $surveyId = $this->surveyId;
        $surveyRange = $this->surveyRange;
        $surveyInfo = $this->surveyInfo;
        $surveyQuestion = $this->surveyQuestion;
        $createTimeStamp = $this->createTimeStamp;
        $publisher_user_id = $this->publisher_user_id;

        //插入range
        $rangeData = [];
        foreach($surveyRange as $userId)
        {
            $rangeData[] = [
                'survey_id' => $surveyId,
                'user_id' => $userId,
                'status' => 1,
            ];
        }
        $rangeIds = (new SurveyRangeService)->saveMany($rangeData, ['survey_id', 'user_id']);
        
        if($surveyInfo['is_temporary'] == 0) {
            (new SurveyRangeLatestService)->saveMany($rangeData, ['survey_id', 'user_id']);
        }
        
        // 添加督办信息
        $inspectAddData = [];
        $i = 0;
        foreach($surveyInfo['inspectList'] ?? [] as $inspectInfo)
        {
            foreach($inspectInfo['inspectUserList'] as $inspectUserInfo)
            {
                if(!empty($inspectUserInfo)) {
                    $inspectUnitInfo = $inspectInfo['unitInfo'];
                    $inspectRangeIdList = ArrayHelper::getColumn($inspectInfo['rangeUserIdList'], 'user_id');
                    
                    $inspectAddData[$i]['inspect_user_id'] = (string)$inspectUserInfo['user_id'];
                    $inspectAddData[$i]['publish_user_id'] = (string)$publisher_user_id;
                    $inspectAddData[$i]['unit_id'] = $inspectUnitInfo['id'];
                    $inspectAddData[$i]['range_id_json'] = json_encode($inspectRangeIdList);
                    $inspectAddData[$i]['type'] = 2; // 1为通知 2为征集
                    $inspectAddData[$i]['relate_id'] = $surveyId;
                    $inspectAddData[$i]['inspect_type'] = $surveyInfo['inspectType'];
                    $inspectAddData[$i]['create_timestamp'] = $createTimeStamp;
                    
                    $i ++;
                }
            }
        }
        (new InspectInfoService)->addMany($inspectAddData);
        
        //插入Question
        $questionData = [];
        foreach($surveyQuestion as $surveyQuestionInfo)
        {
            $questionData[] = [
                'survey_id' => $surveyId,
                'content' => $surveyQuestionInfo['content'],
                'type' => $surveyQuestionInfo['type'],
                'status' => 1,
            ];
        }
        $questionIdList = (new SurveyQuestionService)->addMany($questionData);
        
        $questionOptionData = [];
        foreach($surveyQuestion as $key => $surveyQuestionInfo)
        {
            if($surveyQuestionInfo['type'] != 1) {
                foreach($surveyQuestionInfo['options'] as $optionInfo) {
                    $questionOptionData[] = [
                        'question_id' => $questionIdList[$key],
                        'order' => $optionInfo['order'],
                        'content' => $optionInfo['content'],
                        'status' => 1,
                    ];
                }
            }
        }   
        (new SurveyQuestionOptionService)->addMany($questionOptionData);
        
        //插入remind_info
        $remind_data=[];
        $i=0;
        foreach($surveyInfo['remind_times'] as $key){
            $remind_data[$i]['publisher_id']=$publisher_user_id;
            $remind_data[$i]['is_remind_me']=$surveyInfo['is_remind_me'][$i];
            $remind_data[$i]['is_remind_ontime']=$surveyInfo['is_remind_ontime'][$i];
            $remind_data[$i]['content']=($surveyInfo['remind_contents'][$i] == '') ? $surveyInfo['name'] : $surveyInfo['remind_contents'][$i];
            $remind_data[$i]['relate_id']=$surveyId;
            $remind_data[$i]['type']=2;
            $remind_data[$i]['has_send']=0;
            $remind_data[$i]['timestamp']= $surveyInfo['remind_times'][$i];
            $i++;
        }
        (new RemindInfoService)->addMany($remind_data);
        
        // $inputs = [];
        // $inputs['title'] = '征集---'.$surveyInfo['name'];
        // $inputs['content'] = $surveyInfo['requirement'];
        // $inputs['temporary'] = 0;
        // $inputs['type'] = 0;
        // $inputs['range_ids'] = $surveyRange;
        // $ifDeleteTemporary = 0;//不删除该用户通知暂存内容
        // self::callModuleService('notify', 'NotifyService', 'addOne', $inputs, $publisher_user_id, $ifDeleteTemporary);

        //完成所有工作，修改进度
        (new SurveyInfoService)->update(['process'=>2],['id' => $surveyId]);    //阅读中
        if($surveyInfo['is_temporary'] == 0) { // 修改latest
            (new SurveyInfoLatestService)->update(['process'=>2],['id' => $surveyId]);
        }

     }
}